#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        string s;
        cin >> s;
        int n = s.size();
        vector<int> ls(n, -1);
        vector<int> rs(n, n);

        int last = -1;
        for (int i = 0; i < n; i++) {
            ls[i] = last;
            if (s[i] == '*') {
                last = i;
            }
        }

        last = n;
        for (int i = n-1; i >= 0; i--) {
            rs[i] = last;
            if (s[i] == '*') {
                last = i;
            }
        }

        long long ans = 0;
        for (int pos = 0; pos < n; pos++) {
            if (s[pos] != '*') continue;

            int lb = (ls[pos] == -1) ? 0 : ls[pos] + 1;
            int rb = (rs[pos] == n) ? n-1 : rs[pos] - 1;

            map<int, int> lm;
            int cl = 0, mincl = 0;

            for (int i = pos-1; i >= lb; i--) {
                int val = (s[i] == '(') ? 1 : -1;
                int nc = val + cl;
                int nmin = min(val, val + mincl);
                cl = nc;
                mincl = nmin;

                if (s[i] == '(' && mincl >= 0) {
                    lm[cl]++;
                }
            }

            map<int, int> cntr;
            int cr = 0;
            int mincr = 0;
            for (int j = pos+1; j <= rb; j++) {
                int val = (s[j] == '(') ? 1 : -1;
                cr += val;
                mincr = min(mincr, cr);
                if (s[j] == ')' && mincr == cr) {
                    cntr[cr]++;
                }
            }

            for (const auto& p : lm) {
                int x = p.first;
                auto it = cntr.find(-x);
                if (it != cntr.end()) {
                    ans += p.second * it->second;
                }
            }
        }
        cout << ans << '\n';
    }
}